<template>
  <a-modal
      title="编辑商品分类"
      :width="720"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :destroyOnClose="true"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
              v-decorator="['catName', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入分类名称"
          />
        </a-form-item>
        <a-form-item label="上级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tree-select
              v-decorator="['parentNo']"
              :disabled="disabled"
              :treeData="categoryListTree"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              allowClear
              placeholder="请选择上级分类"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'
import * as Api from '@/api/category'
import CategoryModel from '@/common/model/Category'

export default {
  props: {
    // 分类列表
    categoryList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 标签布局属性
      labelCol: {span: 7},
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 上级分类列表
      categoryListTree: [],
      // 当前记录
      record: {},
      disabled: false
    }
  },
  methods: {

    // 显示对话框
    rename(record) {
      // 显示窗口
      this.visible = true
      // 当前分类记录
      this.record = record
      // 获取分类列表
      this.getCategoryList()
      // 设置默认值
      this.setFieldsValue()
    },

    // 设置默认值
    setFieldsValue() {
      const self = this;
      const {record, form: {setFieldsValue}} = self
      // 设置表单内容
      this.$nextTick(() => {
        setFieldsValue(_.pick(record, ['catName', 'parentNo']))
        self.disabled = true
      })
    },

    // 获取分类列表
    getCategoryList() {
      const {categoryList} = this
      // 顶级分类
      this.categoryListTree = CategoryModel.formatTreeData(categoryList)
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {form: {validateFields}} = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    // 关闭对话框事件
    handleCancel() {
      this.visible = false
      this.form.resetFields()
      this.disabled = false
    },

    // 提交到后端api
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.rename(this.record['id'], values)
          .then(result => {
            // 显示成功
            this.$message.success('操作成功', 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally(result => {
            this.confirmLoading = false
          })
    }
  }
}
</script>
